<template>
    <div class="p-2">
        <form action="#" style="width: 400px;">
            <div class="form-group">
                <label class="form-control-label">客户名称</label>
                <input type="text" v-model="formData.title" class="form-control" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label class="form-control-label">负责人姓名（校长）</label>
                <input type="text" v-model="principal.name" class="form-control" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label class="form-control-label">负责人手机</label>
                <input type="text" v-model="principal.telephone" class="form-control" placeholder="请输入用户名">
            </div>
            <div class="form-group">
                <label class="form-control-label">地区</label>
                <input type="text" v-model="formData.area" class="form-control" placeholder="用于登录">
            </div>
            <div class="form-group">
                <label class="form-control-label">类型</label>

                <div class="wallet-method">
                    <label class="radio-inline custom_radio me-4">
                        <input type="radio" name="optradio" v-model="formData.type" value="training">
                        <span class="checkmark"></span> 培训结构
                    </label>
                    <label class="radio-inline custom_radio me-4">
                        <input type="radio" name="optradio" v-model="formData.type" value="school">
                        <span class="checkmark"></span> 学校
                    </label>
                    <label class="radio-inline custom_radio">
                        <input type="radio" name="optradio" v-model="formData.type" value="studio">
                        <span class="checkmark"></span> 个人工作室
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="form-control-label">有效期</label>
                <input type="text" class="form-control" v-model="formData.endAt" placeholder="有效期">
            </div>
            <div class="form-group">
                <label class="form-control-label">简介</label>
                <textarea rows="5" v-model="formData.info" class="form-control">

                                </textarea>
            </div>

            <div class="form-group">
                <button type="button" @click="save" class="btn btn-primary">确认</button>
            </div>
        </form>
    </div>
</template>

<script setup>
const route = useRoute()
import customApi from '@/api/customApi'
import masterApi from "@/api/masterApi"
const originTitle = ref(null)
const formData = reactive({
    id: '',
    title: 'xx机构',
    logo: '', // logo
    website: '', // 域名
    info: '最好的培训机构',
    type: 'training',
    endAt: '2025-01-01' // 截止时间
})
const principal = reactive({
    id: '',
    name: '',
    telephone: '',
    tenantID: ''
})
onMounted(async () => {

    await nextTick();
    const id = route.query.id
    // 获取详情
    // 获取课程信息
    // 获取教师信息
    // 获取学员信息
    // 获取订单信息
    // 获取备忘录
    const data = await customApi.getDetail(id)
    formData.id = data.id
    formData.title = data.title
    originTitle.value = data.title
    formData.logo = data.logo
    formData.website = data.website
    principal.id = data.principal.id
    principal.telephone = data.principal.telephone
    principal.tenantID = data.principal.tenantID
    principal.name = data.principal.name
    formData.info = data.info
    formData.type = data.type
    formData.endAt = data.endAt.substring(0, 10)
})
const checkFormat = () => {
    if (/^\d{4}-\d{2}-\d{2}$/.test(formData.endAt) === false) {
        alert('日期格式错误')
        return false;
    }

    if (!formData.title) {
        alert('机构名称不能为空')
        return false;
    }
    if (!principal.name) {
        alert('校长的名称不能为空')
        return false;
    }
    if (!formData.website) {
        alert('机构域名不能为空')
        return false;
    }
    if (!formData.info) {
        alert('机构简介不能为空')
        return false;
    }
    return true
}
const save = async () => {
    if (!checkFormat()) return;
    const id = formData.id
    await customApi.put(id, formData)
    if (principal.id && principal.tenantID) {
        // 之前就有校长，现在是更新
        await masterApi.put(principal.id, principal)
    }

    await masterApi.add({
        name: principal.name,
        telephone: principal.telephone,
        tenantID: id
    })



}
const cancel = () => {
    formData.title = originTitle.value
}
</script>